package com.intelligent.mts.client.core.view;

import com.google.common.collect.Lists;
import com.intelligent.mts.client.MainFrame;
import com.intelligent.mts.client.core.controller.MainController;
import com.intelligent.mts.client.event.StatusBarChangeEvent;
import com.intelligent.mts.client.ex.MyDialog;
import com.intelligent.mts.client.utils.*;
import com.intelligent.mts.common.utils.ApplicationException;
import com.intelligent.mts.common.utils.ExceptionMessages;
import com.intelligent.mts.common.utils.StringUtils;
import com.intelligent.mts.core.dto.CustomerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Garen.Pang on 2016/8/19.
 */
@Component
public class BomManagePanel extends JPanel{

    @Autowired
    public MainController mainController;

    public MyDialog myDialog;

    @PostConstruct
    private void initComponents() {

        addCustomer = new JButton();
        addFGPN = new JButton();

        addCustomer.setText("Add New Customer");
        addCustomer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnAddNewCustomerActionPerformed(e);
            }
        });
        addFGPN.setText("Add New Finish Good");
        addFGPN.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnAddNewFinishActionPerformed(e);
            }
        });

        this.setLayout(new GridBagLayout());

        this.add(addCustomer, new GBC(0, 0).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(0, 0, 0, 5));
        this.add(addFGPN, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(1, 0).setInsets(0, 0, 0, 5));
        this.add(new JPanel(), new GBC(2, 0).setFill(GBC.HORIZONTAL).setWeight(20, 0).setInsets(0, 0, 0, 5));

    }

    private void btnAddNewCustomerActionPerformed(ActionEvent evt){


        try {

            Object o = ApplicationBeanUtils.getBean(MainFrame.class);

            MyDialog dlg = new MyDialog((Frame) o,true, "customer",null);
            myDialog = dlg;
            dlg.show();

        }catch (ApplicationException ex){

            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ex.getMessage()), StatusBarChangeEvent.MSG_LEVEL_ERROR));
        }
    }

    private void btnAddNewFinishActionPerformed(ActionEvent evt){

        try {
        java.util.List<CustomerDto> list = Lists.newArrayList();
        list = mainController.findAllCustomer();

            Object o = ApplicationBeanUtils.getBean(MainFrame.class);

            MyDialog dlg = new MyDialog((Frame) o,true, "fgpn" ,initCustomerData(list));

            myDialog = dlg;
            dlg.show();

        }catch (ApplicationException ex){
            if(ResourceBundleUtils.getString(ex.getMessage()) == null || "".equals(ResourceBundleUtils.getString(ex.getMessage()))){
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTERM_ERROR), StatusBarChangeEvent.MSG_LEVEL_ERROR));
            }else
                EventBusUtils.post(new StatusBarChangeEvent(ResourceBundleUtils.getString(ex.getMessage()), StatusBarChangeEvent.MSG_LEVEL_ERROR));
        }
    }

    private Object[] initCustomerData(java.util.List<CustomerDto> list){
        int length = list.size();
        Object[] value = new Object[length];
        for (int i = 0; i < length; i++) {
            value[i] = list.get(i).getName();
        }
        return value;
    }


    public JButton getAddFGPN() {
        return addFGPN;
    }

    public void setAddFGPN(JButton addFGPN) {
        this.addFGPN = addFGPN;
    }

    private JButton addCustomer;
    private JButton addFGPN;

}
